package com.example.yuejian.activity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.example.yuejian.R;
import com.example.yuejian.adapter.ClubAdapter;
import com.example.yuejian.adapter.OrderAdapter;
import com.example.yuejian.bean.ClubEntity;
import com.example.yuejian.bean.OrderEntity;
import com.example.yuejian.bean.TrainEntity;
import com.example.yuejian.common.Url;
import com.example.yuejian.view.TitleView;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class OrderActivity extends BaseActivity {


    @BindView(R.id.recy)
    RecyclerView recyclerView;

    RecyclerView.Adapter adapter;

    LinearLayoutManager layoutManager;
    private List<OrderEntity> entityList;
    private Context context;

    @BindView(R.id.title_activity)
    TitleView titleView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_order);

        super.onCreate(savedInstanceState);

        context = getBaseContext();
        initView();

        all();

    }
    private String uid;

    private void initView() {
        titleView.getLeftBackTextTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ActivityUtils.finishActivity(OrderActivity.class);
            }
        });
        uid = SPUtils.getInstance().getInt("uid")+"";
        entityList = new ArrayList<>();

        adapter = new OrderAdapter(context, entityList, R.layout.class_item);

        layoutManager = new LinearLayoutManager(context);

        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
//设置为垂直布局，这也是默认的
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
//设置Adapter
        recyclerView.setAdapter(adapter);
//设置增加或删除条目的动画
        recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }


    Handler handler_activity = new mHandler();
    @OnClick(R.id.yiwancheng)
    void yiwancheng() {

        EasyHttp.get("yuejian6/public/index.php/json/my/endorder").params("user_id",uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }
    @OnClick(R.id.all)
    void all(){
        EasyHttp.get("yuejian6/public/index.php/json/my/order").params("user_id",uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    @OnClick(R.id.weiwancheng)
    void weiwancheng() {

        EasyHttp.get("yuejian6/public/index.php/json/my/waitorder").params("user_id",uid)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onSuccess(String data) {

                        Message msg = Message.obtain(); // 实例化消息对象
                        msg.what = 0; // 消息标识
                        msg.obj = data; // 消息内容存放

                        handler_activity.sendMessage(msg);
                    }

                    @Override
                    public void onError(ApiException error) {
                        Log.d(TAG, "onFailed: " + error);
                    }
                });
    }

    private static final String TAG = "TrainActivity";
    class mHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case 0:
                    entityList.clear();

                    List<OrderEntity> entities = null;

                    try{
                        entities = JSON.parseArray(msg.obj.toString(),OrderEntity.class);
                    }catch (Exception e){
                        ToastUtils.showShort("获取数据失败");
                    }

                    if (entities==null){
                        return;
                    }

                    entityList.addAll(entities);

                    adapter.notifyDataSetChanged();
                    break;
            }

        }
    }



}
